﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary;
using CommonLibrary.Forms;
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;

namespace BusinessLibrary
{
    /// <summary>
    /// PLCTask
    /// </summary>
    public class PLCTask : IPlugin
    {
        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description => "PLCTask";
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="pluginParam">参数</param>
        /// <returns></returns>
        public string Excute(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            if (pluginParam != null && pluginParam.Identity.ToUpper().Equals("PLCTASK_READ_PARAMS"))
            {
                result = ReadParams(pluginParam);
            }
            return result;
        }
        /// <summary>
        /// 返回一个窗口
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>

        public HWFrmWithTitle GetForm(PluginParamEntity pluginParam)
        {
            return null;
        }
        /// <summary>
        /// 返回一个 Bitmap
        /// </summary>
        /// <returns></returns>
        public Bitmap GetBitmap()
        {
            return null;
        }
        /// <summary>
        /// VERSION_VERSION
        /// 程序版本
        /// </summary>
        /// <returns></returns>
        private string ReadParams(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            string methodName = "读取PLC参数配置";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PLC参数地址配置.xlsx");
            bool isExists = File.Exists(path);
            var backupDir = "D:\\PLC参数备份";

            this.Info($"【{pluginParam.Tag}】文件：{path}，{(isExists ? "存在" : "不存在")}");
            if (isExists)
            {
                try
                {
                    var list = CommonHelper.ReadExcel<PLCTagSetting>(path);
                    this.Info($"【{pluginParam.Tag}】处理 - {methodName} - 获取到的参数个数：{(list == null || list.Count == 0 ? 0 : list.Count)}");
                    if (list != null && list.Count > 0)
                    {
                        if (APPInstance.IsPLCConnected)
                        {
                            this.Info($"【{pluginParam.Tag}】处理 - {methodName} - 开始读取PLC");
                            var datas = APPInstance.PLC.BatchRead(list);
                            this.Info($"【{pluginParam.Tag}】处理 - {methodName} - 结束读取PLC");
                            if (datas != null && datas.Count > 0)
                            {
                                this.Info($"【{pluginParam.Tag}】处理 - {methodName} - 开始写入Excel文档");
                                string outputPath = $"{backupDir}\\PLC参数备份{DateTime.Now.ToString("yyyy-MM-dd_HH")}.xlsx";
                                CommonHelper.WriteExcel(outputPath, datas, true);
                                this.Info($"【{pluginParam.Tag}】处理 - {methodName} - 结束写入Excel文档");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.Error(ex);
                }
            }

            #region 删除旧文档

            this.Info($"【{pluginParam.Tag}】删除旧文档开始 - {methodName}");
            if (Directory.Exists(backupDir))
            {
                var temp = new DirectoryInfo(backupDir);
                try
                {
                    var files = temp.GetFiles("*.xlsx").Where(v => DateTime.Now.Subtract(v.CreationTime).TotalDays >= 10).ToList();
                    if (files != null)
                    {
                        foreach (var file in files)
                        {
                            file.DeleteFile();
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.Error(ex);
                }
            }
            this.Info($"【{pluginParam.Tag}】删除旧文档结束 - {methodName}");
            #endregion

            this.Info($"【{pluginParam.Tag}】结束 - {methodName}");
            return result;
        }
    }
}
